package com.example.administrator.xinyutg.fragment;


import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

import com.example.administrator.xinyutg.R;
import com.example.administrator.xinyutg.adapter.AdPagerAdapter;
import com.example.administrator.xinyutg.adapter.DataAdapter;
import com.example.administrator.xinyutg.bean.AdBean;
import com.example.administrator.xinyutg.bean.GroupData;
import com.example.administrator.xinyutg.costants.Path;
import com.example.administrator.xinyutg.task.AdTask;
import com.example.administrator.xinyutg.task.Callback;
import com.example.administrator.xinyutg.task.DataDownTask;
import com.example.administrator.xinyutg.task.ImageTask;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class GroupFragment extends Fragment {


    private ListView lvGroup;
    private List<GroupData> dataList;
    private DataAdapter adapter;

    //顶部广告图片相关
    private RelativeLayout layout;
    private ViewPager vpAd;
    private List<AdBean> adList;
    private List<ImageView> vpList;
    private AdPagerAdapter adAdapter;
    private AdTask adTask;
    private List<AdBean> gv1List;
    private List<ImageView> gvImgList;

    //GridView相关
    private int[] icon = {R.mipmap.ic_category_0, R.mipmap.ic_category_5, R.mipmap.ic_category_1, R.mipmap.ic_category_3, R.mipmap.ic_category_7, R.mipmap.ic_category_9, R.mipmap.ic_category_2, R.mipmap.ic_category_15};
    private String[] iconName = {"餐饮美食", "代金卷", "电影", "KTV", "休闲娱乐", "生活服务", "酒店宾馆", "全部分类"};
    private GridView gvGroup;
    private List<Map<String, Object>> gvList;
    private Object data;
    private SimpleAdapter gvAdapter;
    private GridView gvInfoGroup;
    private ArrayAdapter infoAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_group, container, false);

        initView(v);
        //加载广告图片和GridView布局
        addAd(inflater);

        adapter = new DataAdapter(dataList, getActivity());
        initData();
        lvGroup.setAdapter(adapter);

        return v;
    }

    private void initData() {
        DataDownTask task = new DataDownTask(dataList, adapter);
        task.execute(Path.PATH_GROUP);
    }

    private void initView(View v) {
        lvGroup = (ListView) v.findViewById(R.id.lv_group);
        dataList = new ArrayList<GroupData>();
    }


    private void addAd(LayoutInflater inflater) {
        //动态加载广告图片
        layout = (RelativeLayout) inflater.inflate(R.layout.fragment_ad_layout, null);
        //得到viewpager
        vpAd = (ViewPager) layout.findViewById(R.id.vp_ad);
        adList = new ArrayList<AdBean>();
        gv1List=new ArrayList<AdBean>();
        adTask = new AdTask(adList,gv1List ,new Callback() {
            @Override
            public void success() {
                vpList = new ArrayList<ImageView>();
                for (int i = 0; i < adList.size(); i++) {
                    ImageView iv = new ImageView(getActivity());
                    AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.MATCH_PARENT);
                    iv.setLayoutParams(params);
                    //设置图片
                    iv.setImageResource(R.mipmap.ic_launcher);
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    //下载图片
                    iv.setTag(adList.get(i).getImage());
                    new ImageTask(iv).execute(Path.PATH_PREFIX + adList.get(i).getImage());
                    vpList.add(iv);
                }
                //创建适配器
                adAdapter = new AdPagerAdapter(vpList);
                vpAd.setAdapter(adAdapter);

                gvImgList=new ArrayList<ImageView>();
                for (int i = 0; i < gv1List.size(); i++) {
                    ImageView iv = new ImageView(getActivity());
                    AbsListView.LayoutParams params = new AbsListView.LayoutParams(100, 80);
                    iv.setLayoutParams(params);
                    //设置图片
                    iv.setImageResource(R.mipmap.ic_launcher);
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    //下载图片
                    iv.setTag(gv1List.get(i).getImage());
                    new ImageTask(iv).execute(Path.PATH_PREFIX + gv1List.get(i).getImage());
                    gvImgList.add(iv);
                }
                gvInfoGroup = (GridView) layout.findViewById(R.id.gv_group_info);
                infoAdapter = new ArrayAdapter(getActivity(), R.layout.item_gridview,R.id.gv_info_img,gvImgList);
                gvInfoGroup.setAdapter(infoAdapter);
            }
        });
        adTask.execute(Path.PATH_TOP);

        //GridView布局
        gvGroup = (GridView) layout.findViewById(R.id.gv_group);
        getData();
        gvAdapter = new SimpleAdapter(getActivity(),gvList, R.layout.item_gridview_layout,new String[]{"image","name"},new int[]{R.id.image,R.id.name});
        gvGroup.setAdapter(gvAdapter);

       /* gvInfoGroup = (GridView) layout.findViewById(R.id.gv_group_info);
        infoAdapter = new ArrayAdapter(getActivity(), R.layout.item_gridview,R.id.gv_info_img,gvImgList);
        gvInfoGroup.setAdapter(infoAdapter);*/


        lvGroup.addHeaderView(layout);
    }

    public List<Map<String,Object>> getData() {
        gvList = new ArrayList<Map<String,Object>>();
        for (int i = 0; i < icon.length; i++) {
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("image",icon[i]);
            map.put("name",iconName[i]);
            gvList.add(map);
        }
        return gvList;
    }
}
